{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter 0,Testing Accuracy 0.9117,Training Accuracy 0.903655\n",
      "Iter 1,Testing Accuracy 0.9243,Training Accuracy 0.923709\n",
      "Iter 2,Testing Accuracy 0.936,Training Accuracy 0.933528\n",
      "Iter 3,Testing Accuracy 0.9389,Training Accuracy 0.939546\n",
      "Iter 4,Testing Accuracy 0.9419,Training Accuracy 0.942328\n",
      "Iter 5,Testing Accuracy 0.9466,Training Accuracy 0.948309\n",
      "Iter 6,Testing Accuracy 0.9487,Training Accuracy 0.951873\n",
      "Iter 7,Testing Accuracy 0.9514,Training Accuracy 0.953437\n",
      "Iter 8,Testing Accuracy 0.9525,Training Accuracy 0.956291\n",
      "Iter 9,Testing Accuracy 0.9533,Training Accuracy 0.957327\n",
      "Iter 10,Testing Accuracy 0.9558,Training Accuracy 0.958237\n",
      "Iter 11,Testing Accuracy 0.9571,Training Accuracy 0.961128\n",
      "Iter 12,Testing Accuracy 0.9576,Training Accuracy 0.963473\n",
      "Iter 13,Testing Accuracy 0.9595,Training Accuracy 0.964037\n",
      "Iter 14,Testing Accuracy 0.9604,Training Accuracy 0.965255\n",
      "Iter 15,Testing Accuracy 0.9619,Training Accuracy 0.966\n",
      "Iter 16,Testing Accuracy 0.9621,Training Accuracy 0.966928\n",
      "Iter 17,Testing Accuracy 0.9634,Training Accuracy 0.968091\n",
      "Iter 18,Testing Accuracy 0.9643,Training Accuracy 0.968691\n",
      "Iter 19,Testing Accuracy 0.9659,Training Accuracy 0.970237\n",
      "Iter 20,Testing Accuracy 0.9656,Training Accuracy 0.971128\n",
      "Iter 21,Testing Accuracy 0.9653,Training Accuracy 0.971218\n",
      "Iter 22,Testing Accuracy 0.9667,Training Accuracy 0.971873\n",
      "Iter 23,Testing Accuracy 0.9676,Training Accuracy 0.973655\n",
      "Iter 24,Testing Accuracy 0.9675,Training Accuracy 0.973546\n",
      "Iter 25,Testing Accuracy 0.9695,Training Accuracy 0.974637\n",
      "Iter 26,Testing Accuracy 0.97,Training Accuracy 0.975618\n",
      "Iter 27,Testing Accuracy 0.9688,Training Accuracy 0.9752\n",
      "Iter 28,Testing Accuracy 0.969,Training Accuracy 0.976928\n",
      "Iter 29,Testing Accuracy 0.9695,Training Accuracy 0.976491\n",
      "Iter 30,Testing Accuracy 0.971,Training Accuracy 0.977219\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "#载入数据集\n",
    "mnist = input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "#每个批次的大小\n",
    "batch_size = 100\n",
    "#计算一共有多少个批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "#定义两个placeholder\n",
    "x = tf.placeholder(tf.float32,[None,784])\n",
    "y = tf.placeholder(tf.float32,[None,10])\n",
    "keep_prob=tf.placeholder(tf.float32)\n",
    "\n",
    "#创建一个简单的神经网络\n",
    "W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))\n",
    "b1 = tf.Variable(tf.zeros([2000])+0.1)\n",
    "L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)\n",
    "L1_drop = tf.nn.dropout(L1,keep_prob) \n",
    "\n",
    "W2 = tf.Variable(tf.truncated_normal([2000,2000],stddev=0.1))\n",
    "b2 = tf.Variable(tf.zeros([2000])+0.1)\n",
    "L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)\n",
    "L2_drop = tf.nn.dropout(L2,keep_prob) \n",
    "\n",
    "W3 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1))\n",
    "b3 = tf.Variable(tf.zeros([1000])+0.1)\n",
    "L3 = tf.nn.tanh(tf.matmul(L2_drop,W3)+b3)\n",
    "L3_drop = tf.nn.dropout(L3,keep_prob) \n",
    "\n",
    "W4 = tf.Variable(tf.truncated_normal([1000,10],stddev=0.1))\n",
    "b4 = tf.Variable(tf.zeros([10])+0.1)\n",
    "prediction = tf.nn.softmax(tf.matmul(L3_drop,W4)+b4)\n",
    "\n",
    "#二次代价函数\n",
    "# loss = tf.reduce_mean(tf.square(y-prediction))\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))\n",
    "#使用梯度下降法\n",
    "train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "#初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "#结果存放在一个布尔型列表中\n",
    "correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置\n",
    "#求准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(31):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys =  mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})\n",
    "        \n",
    "        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})\n",
    "        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})\n",
    "        print(\"Iter \" + str(epoch) + \",Testing Accuracy \" + str(test_acc) +\",Training Accuracy \" + str(train_acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Iter 29,Testing Accuracy 0.9727,Training Accuracy 0.995655\n",
    "Iter 30,Testing Accuracy 0.9722,Training Accuracy 0.995782"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
